﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using VupSlashLauncher.Common;

namespace VupSlashLauncher.View
{
    public partial class IInputBox : Window
    {

        /// <summary>
        /// 弹出消息框，显示提示信息
        /// </summary>
        /// <param name="title">弹窗标题</param>
        /// <param name="msg">提示信息</param>
        /// <returns></returns>
        public static string Show(string msg, string title = null)
        {
            IInputBox msgBox = new IInputBox() { Title = title, Message = msg };
            msgBox.ShowDialog();
            return output;
        }

        private IInputBox()
        {
            InitializeComponent();
        }

        private static string output = null;

        public new string Title
        {
            get { return this.TitleLabel.Text; }
            set { this.TitleLabel.Text = value; }
        }
        public string Message
        {
            get { return this.MsgLabel.Text; }
            set { this.MsgLabel.Text = value; }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Resources.Add("TColor", new SolidColorBrush((Color)ColorConverter.ConvertFromString(GlobalValues.ThemeColor)));
            Color c = (Color)ColorConverter.ConvertFromString((GlobalValues.ThemeColor));
            if ((c.R * 30 + c.G * 59 + c.B * 11 + 50) / 100 > 160) { this.Resources.Add("CColor", new SolidColorBrush(Color.FromRgb(0, 0, 0))); }
            else { this.Resources.Add("CColor", new SolidColorBrush(Color.FromRgb(255, 255, 255))); }
            this.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() => { Keyboard.Focus(InputLabel); }));
        }

        private void WindowMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.DragMove();
        }
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            output = null;
            var story = (Storyboard)this.Resources["WindowDisappearAnimation"];
            if (story != null)
            {
                story.Completed += delegate { Close(); };
                story.Begin(this);
            }
        }
        private void YesButton_Click(object sender, RoutedEventArgs e)
        {
            output = InputLabel.Text;
            var story = (Storyboard)this.Resources["WindowDisappearAnimation"];
            if (story != null)
            {
                story.Completed += delegate { Close(); };
                story.Begin(this);
            }
        }
        private void InputLabel_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter) YesButton_Click(null, null);
        }

        protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.Space)
            {
                e.Handled = true;
            }
            else if (Keyboard.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.F4)
            {
                e.Handled = true;
                CloseButton_Click(null, null);
            }
            else if (e.Key == Key.Escape) CloseButton_Click(null, null);
        }

    }
}